package com.qf.smsplatfrom.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smsplatfrom.annotations.IgnoreLog;
import com.qf.smsplatfrom.annotations.LogOperationAnno;
import com.qf.smsplatfrom.dto.R;
import com.qf.smsplatfrom.pojo.TAdminUser;
import com.qf.smsplatfrom.pojo.TBlackList;
import com.qf.smsplatfrom.service.TBlackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/black")
@Api(tags = "黑名单管理")
public class TBlackListController {
    @Autowired
    private TBlackListService tBlackListService;

    @GetMapping("/black/{id}")
    @IgnoreLog
    public R showById(@PathVariable Long id){
        TBlackList tBlackList = tBlackListService.showById(id);
        R r = R.setOk(tBlackList);
        return r;
    }
    @PostMapping("/add/{mobile}")
    public R addTBlackList(@PathVariable String mobile,@ApiIgnore HttpSession session){
        TBlackList tBlackList = new TBlackList();
        tBlackList.setMobile(mobile);
        Object user = session.getAttribute("user");
        if(user!=null){
            long id = ((TAdminUser) user).getId();//创建者的 id
            Long type = ((TAdminUser) user).getType();//创建者类型
            tBlackList.setCreater(id);
            tBlackList.setOwntype(type);
        }
        tBlackListService.addTBlackList(tBlackList);
        return R.setOk();
    }
    @GetMapping("/show")
    @ApiOperation("分页查询所有数据")
    @LogOperationAnno("分页查询黑名单的数据")
    public R showAll(@RequestParam(defaultValue = "1") @ApiParam(name ="pageNum",value = "页码") Integer pageNum, @RequestParam(defaultValue = "10")  @ApiParam(name ="pageSize",value = "每页条数") Integer pageSize){
        PageInfo<TBlackList> pageInfo = tBlackListService.showAll(pageNum, pageSize);
        return R.setOk(pageInfo);
    }
    @PutMapping("/black")
    public R updateBlack(@RequestBody TBlackList tBlackList,HttpSession session){
        Object user = session.getAttribute("user");
        if(user!=null){
            long id = ((TAdminUser) user).getId();
            long type = ((TAdminUser) user).getType();
            tBlackList.setCreater(id);
            tBlackList.setOwntype(type);
        }
        tBlackListService.updateBlack(tBlackList);
        return R.setOk();
    }
    @DeleteMapping("/black")
    public R deleteBlack(@RequestBody Map map){
        tBlackListService.deleteBlack(((List<Integer>) map.get("ids")));
        return R.setOk();
    }
}
